package com.gking.centerSystem.service.SystemInfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.centerSystem.common.BaseContext;
import com.gking.centerSystem.common.ProgramException;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.RootSystem;
import com.gking.centerSystem.entity.Sort;
import com.gking.centerSystem.entity.SystemInfo;
import com.gking.centerSystem.mapper.RootSystemMapper;
import com.gking.centerSystem.mapper.SystemInfoMapper;
import com.gking.centerSystem.service.Sort.SortService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SystemInfoServiceImpl extends ServiceImpl<SystemInfoMapper, SystemInfo> implements SystemInfoService {


    @Resource
    private RootSystemMapper rootSystemMapper;

    @Resource
    private SystemInfoMapper systemInfoMapper;

    /**
     * 获取系统ip列表
     *
     * @return l
     */
    @Override
    public Result<List<String>> getSystemIpList() {
        QueryWrapper<SystemInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable", 0);
        queryWrapper.eq("isDelete", 0);
        queryWrapper.select("systemIP");
        List<SystemInfo> list = list(queryWrapper);

        List<String> l = new ArrayList<>();
        for (SystemInfo systemInfo : list) l.add(systemInfo.getSystemIP());

        return Result.success("获取系统 ip 列表成功", l);
    }

    /**
     * 获取系统信息列表
     *
     * @param systemCode 系统编码
     * @param size       分页大小
     * @param page       分页页码
     * @return s
     */
    @Override
    public Result<Page> getSystemInfoList(Integer page, Integer size, String systemCode) {
        try {
            Page<SystemInfo> pageInfo = new Page<>(page, size);
            Page<SystemInfo> systemInfoPage = (Page<SystemInfo>) systemInfoMapper.getSystemInfoPage(pageInfo, systemCode);
            return Result.success("获取系统信息列表成功！", systemInfoPage);
        } catch (Exception e) {
            log.error("获取系统信息列表失败，原因：{}", e.getMessage());
            throw new ProgramException();
        }
    }


    @Override
    public Result<String> setSystemInfo(String systemInfoId, String systemCode, String systemIp, String remark) {

        if (systemIp.isEmpty())
            return Result.error("请将参数补充完整");

        if (Objects.equals(systemInfoId, "")) systemInfoId = null;

        try {
            LambdaQueryWrapper<RootSystem> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RootSystem::getSystemCode, systemCode);
            wrapper.eq(RootSystem::getIsDelete, 0);
            RootSystem rootSystem = rootSystemMapper.selectOne(wrapper);
            if (rootSystem == null) return Result.error("系统编码不存在！");

            SystemInfo systemInfo = new SystemInfo();
            systemInfo.setSystemCode(rootSystem.getSystemCode());
            systemInfo.setSystemName(rootSystem.getSystemName());
            systemInfo.setSystemIP(systemIp);
            systemInfo.setRemark(remark);

            if (systemInfoId == null) {
                systemInfo.setCreateUserId(BaseContext.getCurrentId());
                this.save(systemInfo);
                return Result.success("创建系统信息成功！");
            }

            systemInfo.setId(systemInfoId);
            this.updateById(systemInfo);
            return Result.success("修改系统信息成功！");
        } catch (Exception e) {
            log.error("设置系统信息失败，原因：{}", e.getMessage());
            throw new ProgramException();
        }
    }


    /**
     * 批量删除系统信息
     *
     * @param systemInfoIdList 系统信息id列表
     * @return s
     */
    @Override
    public Result<String> deleteSystemInfo(List<String> systemInfoIdList) {
        try {
            if (systemInfoIdList.isEmpty()) return Result.error("请选择要删除的系统信息");

            LambdaUpdateWrapper<SystemInfo> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(SystemInfo::getIsDelete, 1);
            wrapper.set(SystemInfo::getDeleteUserId, BaseContext.getCurrentId());
            wrapper.set(SystemInfo::getDeleteTime, LocalDateTime.now());
            wrapper.in(SystemInfo::getId, systemInfoIdList);

            this.update(wrapper);
            return Result.success("删除系统信息成功！");
        } catch (Exception e) {
            log.error("删除系统信息失败，原因：{}", e.getMessage());
            throw new ProgramException();
        }
    }


    /**
     * 批量更新系统信息状态
     *
     * @param systemInfoIdList 系统信息 id 列表
     * @param status           状态 0启用，1禁用
     * @return s
     */
    @Override
    public Result<String> updateSystemInfoStatus(List<String> systemInfoIdList, Integer status) {
        try {
            if (systemInfoIdList.isEmpty() || status == null) return Result.error("请将参数补充完整");
            if (status < 0 || status > 1) return Result.error("状态参数错误");

            LambdaUpdateWrapper<SystemInfo> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(SystemInfo::getEnable, status);
            wrapper.set(SystemInfo::getUpdateUserId, BaseContext.getCurrentId());
            wrapper.set(SystemInfo::getUpdateTime, LocalDateTime.now());
            wrapper.in(SystemInfo::getId, systemInfoIdList);

            this.update(wrapper);
            return Result.success("更新系统信息状态成功！");
        } catch (Exception e) {
            log.error("更新系统信息状态失败，原因：{}", e.getMessage());
            throw new ProgramException();
        }
    }
}
